/*
 * Copyright (C) 2015 tyrantgit
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tyrantgit.explosionfield;

import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.util.Optional;

public class Utils {

    private static final Canvas sCanvas = new Canvas();

    private static final String TAG = "ExplosionDemo_Utils";

    public static DisplayAttributes getScreenPixel(Context context) {
        // 获取屏幕密度
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        DisplayAttributes displayAttributes = display.get().getAttributes();
        return  displayAttributes;
    }

    public static int dp2Px(Context context, float dp) {
        // 获取屏幕密度
        DisplayAttributes displayAttributes = getScreenPixel(context);
        return (int) (dp * displayAttributes.scalDensity);
    }

    public static PixelMap createBitmapFromView(Component view) {

        if (view instanceof Image) {

            return ((Image) view).getPixelMap();

        }

        if (view instanceof Text) {
           Color textColor = ((Text)view).getTextColor();
           PixelMap bitmap = createBitmapSafely(view.getWidth(),
                    view.getHeight(), textColor);

            return bitmap;

        }

        return null;

    }
    public static PixelMap createBitmapSafely(int width, int height, Color color) {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size = new Size(width, height);
        options.pixelFormat = PixelFormat.ARGB_8888;
        options.editable = true;
        int alphaColor = Color.getIntColor("#00000000");
        int colors[] = new int[width * height];
        for (int i = 0; i < width * height; i++) {
            if (i % 5 == 0) {
                colors[i] = color.getValue();
            } else {
                colors[i] = alphaColor;
            }
        }
        return PixelMap.create(colors, options);
    }

    public static PixelMap createBitmapSafely(int width, int height, int retryCount) {
        try {

            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.size = new Size(width, height);
            options.pixelFormat = PixelFormat.ARGB_8888;
            options.editable = true;
            return PixelMap.create(options);

        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            if (retryCount > 0) {
                System.gc();
                return createBitmapSafely(width, height, retryCount - 1);
            }
            return null;
        }
    }

    public static void offset(Rect rect, int dx, int dy) {
        rect.left += dx;
        rect.top += dy;
        rect.right += dx;
        rect.bottom += dy;
    }

    public static void inset(Rect rect, int dx, int dy) {
        rect.left += dx;
        rect.top += dy;
        rect.right -= dx;
        rect.bottom -= dy;
    }
}
